from django.shortcuts import render
from utils.SqlHelper import SqlHelper
from django.core.paginator import Paginator
from  datetime import datetime
from  datetime import timedelta


# Create your views here.

def index(request):
    dicts = getLeftNav()
    conditions = getConditions(request)
    jobs = getJobs(conditions)
    pindex = request.GET.get("pindex")
    pindex = pindex and pindex or 1
    pageSize = 12
    p = Paginator(jobs, pageSize)
    curList = p.page(pindex)
    pages = p.page_range
    dit = {x: y for x, y in request.GET.items()}
    context = {"left": dicts, "jobList": curList, "pindex": int(pindex), "page": pages,"conditions":dit}
    return render(request, "jobsearch/list.html", context)


def getJobs(conditions):
    c = ""
    if conditions[0] and conditions[1]:
        c = " AND ".join(conditions[0])
        # conditions[1]=tuple(conditions[1])
    if c != "":
        c = "WHERE " + c

    sqlHelpder = SqlHelper()
    sql = "SELECT id, o, job_name, company, address, company_type, degree_type, money_s, money_e, money_type, company_size_s, company_size_e, work_age_s, work_age_e, requirement, pub_time, industry_desc, industry_type, benefit, linkman, link_phone, work_address, url  FROM job j %s  ORDER BY  j.pub_time desc LIMIT 1000" % c
    print(sql)
    print(conditions[1])
    # sql = sql % conditions[1]

    lists = sqlHelpder.get_list(sql, conditions[1])
    return lists


def getLeftNav():
    sqlHelpder = SqlHelper()
    sql = "SELECT  typeId, type, `key`, value, `desc` FROM sys_dict sd  WHERE  sd.typeId IN (2,4,6,7)"
    lists = sqlHelpder.get_list(sql)
    dicts = {}
    for obj in lists:
        key = obj["type"] + "_" + str(obj["typeId"])
        if key not in dicts:
            dicts[key] = []
        dicts[key].append(obj)
    return dicts


def getConditions(request):
    lists = []
    vlists = []
    spc = request.GET.get("spc")  # spc 1职位 job_name 4公司 company
    '''kd=前端开发'''
    kd = request.GET.get("kd")
    if spc and kd:
        if spc == "1":
            lists.append("j.job_name like %s")
            vlists.append("%" + kd + "%")
        elif spc == "4":
            lists.append("j.company like %s")
            vlists.append("%" + kd + "%")

    '''
    filter_1001
    <div class="1">今天</div>
                <div class="2">3天内</div>
                <div class="3">一周内</div>
                <div class="4">一月内</div>
    '''

    filter_1001 = request.GET.get("filter_1001")
    if filter_1001:
        if filter_1001 == "1":
            d = datetime.now().strftime("%Y-%m-%d")
            lists.append("j.pub_time=%s")
            vlists.append(d)
        elif filter_1001 == "2":
            e = datetime.now().strftime("%Y-%m-%d")
            s = (datetime.now() - timedelta(days=3)).strftime("%Y-%m-%d")
            lists.append("j.pub_time>=%s and j.pub_time<=%s")
            vlists.append(s)
            vlists.append(e)
        elif filter_1001 == "3":
            e = datetime.now().strftime("%Y-%m-%d")
            s = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d")
            lists.append("j.pub_time>=%s and j.pub_time<=%s")
            vlists.append(s)
            vlists.append(e)
        elif filter_1001 == "4":
            e = datetime.now().strftime("%Y-%m-%d")
            s = (datetime.now() - timedelta(days=30)).strftime("%Y-%m-%d")
            lists.append("j.pub_time>=%s and j.pub_time<=%s")
            vlists.append(s)
            vlists.append(e)
    '''
    filter_1000
    <div class="1">不限</div>
                <div class="2">应届毕业生</div>
                <div class="3">1年以下</div>
                <div class="4">1-3年</div>
                <div class="5">3-5年</div>
                <div class="6">5-10年</div>
                <div class="7">10年以上</div>
    '''
    filter_1000 = request.GET.get("filter_1000")
    if filter_1000:
        if filter_1000 == "2":
            lists.append("j.work_age_s>=%s and j.work_age_e<=%s")
            vlists.append(0)
            vlists.append(0)
        elif filter_1000 == "3":
            lists.append("j.work_age_s=%s and j.work_age_e=%s")
            vlists.append(1)
            vlists.append(-1)
        elif filter_1000 == "4":
            lists.append("j.work_age_s>=%s and j.work_age_e<=%s")
            vlists.append(1)
            vlists.append(3)
        elif filter_1000 == "5":
            lists.append("j.work_age_s>=%s and j.work_age_e<=%s")
            vlists.append(3)
            vlists.append(5)
        elif filter_1000 == "6":
            lists.append("j.work_age_s>=%s and j.work_age_e<=%s")
            vlists.append(5)
            vlists.append(10)
        elif filter_1000 == "7":
            lists.append("j.work_age_s>=%s and j.work_age_e=%s")
            vlists.append(10)
            vlists.append(-1)

    '''
     filter_999
    <div class="1">2k以下</div>
                <div class="2">2k-5k</div>
                <div class="3">5k-10k</div>
                <div class="4">10k-15k</div>
                <div class="5">15k-25k</div>
                <div class="6">25k-50k</div>
                <div class="7">50k以上</div>
    '''
    filter_999 = request.GET.get("filter_999")
    if filter_999:
        if filter_999 == "1":
            lists.append("j.money_s>=%s and j.money_e<=%s")
            vlists.append(2000)
            vlists.append(5000)
        elif filter_1001 == "3":
            lists.append("j.money_s>=%s and j.money_e<=%s")
            vlists.append(5000)
            vlists.append(10000)
        elif filter_1001 == "4":
            lists.append("j.money_s>=%s and j.money_e<=%s")
            vlists.append(10000)
            vlists.append(15000)
        elif filter_1001 == "5":
            lists.append("j.money_s>=%s and j.money_e<=%s")
            vlists.append(15000)
            vlists.append(25000)
        elif filter_1001 == "6":
            lists.append("j.money_s>=%s and j.money_e<=%s")
            vlists.append(25000)
            vlists.append(50000)
        elif filter_1001 == "7":
            lists.append("j.money_s>=%s")
            vlists.append(50000)

    '''
    filter_4  degree_type
    '''
    filter_4 = request.GET.get("filter_4")
    if filter_4:
        lists.append("j.degree_type=%s")
        vlists.append(filter_4)
    '''
    filter_2 company_type
    '''
    filter_2 = request.GET.get("filter_2")
    if filter_2:
        lists.append("j.company_type=%s")
        vlists.append(filter_2)
    '''
    filter_7 o
    '''
    filter_7 = request.GET.get("filter_7")
    if filter_7:
        lists.append("j.o=%s")
        vlists.append(filter_7)
    '''
     filter_6 industry_type
    '''
    filter_6 = request.GET.get("filter_6")
    if filter_6:
        lists.append("j.industry_type=%s")
        vlists.append(filter_6)
    '''
     city address
    '''
    city = request.GET.get("city")
    if city and city != "全国":
        lists.append("j.address like %s")
        vlists.append(city+"%")
    '''
      hot Java PHP Android iOS 前端产品 经理 UI 运营 BD
    '''
    hot = request.GET.get("hot")
    if hot:
        lists.append("j.job_name like %s")
        vlists.append("%" + hot + "%")

    return (lists, tuple(vlists))

    '''
    hot Java PHP Android iOS 前端产品 经理 UI 运营 BD
    kd=前端开发&spc=1&filter_4=&filter_2=&filter_7=&filter_6=&filter_1001=&filter_1000=&filter_999=&city=全国&hot=运营
    '''
